package com.w.wx.service.impls;

import cn.hutool.core.date.DateTime;
import com.w.wx.domain.UserEr;
import com.w.wx.mapper.CommentsInfoMapper;
import com.w.wx.mapper.CommentsReplyMapper;
import com.w.wx.domain.CommentsInfo;
import com.w.wx.domain.CommentsReply;
import com.w.wx.mapper.LikedMapper;
import com.w.wx.mapper.UserErMapper;
import com.w.wx.service.CommeService;
import com.w.wx.utils.ALToHMUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

@Service
@Slf4j
public class CommeServiceImpl implements CommeService {

    @Autowired
    private CommentsInfoMapper commentsInfoMapper;
    @Autowired
    private CommentsReplyMapper commentsReplyMapper;
    @Autowired
    private UserErMapper userErMapper;
    @Autowired
    private LikedMapper likedMapper;

    @Override
    public CommentsInfo addMainComments(CommentsInfo info) {

        UserEr userEr = userErMapper.selectByOpenid(info.getFromOpenid());

        info.setFromAvatar(userEr.getHeadImg());
        info.setFromName(userEr.getWxname());
        info.setUserState(userEr.getState());

        info.setCreateTime(new DateTime());
        info.setLikeNum(0);

        int b = commentsInfoMapper.insert(info);
        if(b==1){

            return commentsInfoMapper.selectByOpenidAndTime(info.getFromOpenid());
        }else {
            return null;
        }

    }

    @Override
    public CommentsReply addSonComments(CommentsReply reply) {
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        //获取被评论者信息

        UserEr userErTo = userErMapper.selectByOpenid(reply.getToOpenid());

        reply.setToName(userErTo.getWxname());
        reply.setToAvatar(userErTo.getHeadImg());

        //获取评论者信息，并设置认证状态
        UserEr userErFrom = userErMapper.selectByOpenid(reply.getFromOpenid());
        reply.setFromAvatar(userErFrom.getHeadImg());
        reply.setFromName(userErFrom.getWxname());
        reply.setUserState(userErFrom.getState());

        reply.setCreateTime(timestamp);
        reply.setLikeNum(0);
        int b = commentsReplyMapper.insert(reply);
        if(b==1){
            return reply;
        }else {
            return null;
        }
    }

    @Override
    public ArrayList<CommentsInfo> getAllComments(int gid,String openid) {
        ArrayList<CommentsInfo> arrayList = commentsInfoMapper.selectAll(gid);
        //如果当前物品有评论
        if (!arrayList.isEmpty()) {
            Iterator<CommentsInfo> it = arrayList.iterator();
            while (it.hasNext()){
                CommentsInfo commentsInfo = it.next();
                Integer i = likedMapper.selectForGetLikedState(commentsInfo.getInId(), openid, 1);

                if (i != null ) {
                    //为null则没有点赞记录，为0则是取消了点赞

                    commentsInfo.setLikeState(i);
                }else{
                    commentsInfo.setLikeState(0);
                }

                commentsInfo.setSonSums(commentsReplyMapper.selectSums(commentsInfo.getInId()));

                ArrayList<CommentsReply> list = commentsReplyMapper.selectByTimeOnlyThree(commentsInfo.getInId());

                Iterator<CommentsReply> iterator = list.iterator();
                while(iterator.hasNext()){
                    CommentsReply commentsReply = iterator.next();
                    Integer i2 = likedMapper.selectForGetLikedState(commentsReply.getRid(), openid, 2);
                    if (i2 != null ) {
                        //为null则没有点赞记录，为0则是取消了点赞
                        commentsReply.setLikeState(i2);
                    }else{
                        commentsReply.setLikeState(0);
                    }
                }


                list.add(new CommentsReply("without"));

                commentsInfo.setCommentsReplies(list);
            }
        }

        return arrayList;
    }

    @Override
    public int getSonCommentsSums(int gid) {
        return commentsReplyMapper.selectSums(gid);
    }

    @Override
    public CommentsInfo getMainAllSonCom(int inId,String openid) {

        CommentsInfo commentsInfo = commentsInfoMapper.selectByPrimaryKey(inId);
        commentsInfo.setContent(ALToHMUtil.toUnicode(commentsInfo.getContent()));
        ArrayList<CommentsReply> list = commentsReplyMapper.selectAllSon(commentsInfo.getInId());

        if(!list.isEmpty()){
            Iterator<CommentsReply> iterator = list.iterator();
            while(iterator.hasNext()){
                CommentsReply commentsReply = iterator.next();
                commentsReply.setContent(ALToHMUtil.toUnicode(commentsReply.getContent()));
                Integer i2 = likedMapper.selectForGetLikedState(commentsReply.getRid(), openid, 2);
                if (i2 != null ) {
                    //为null则没有点赞记录，为0则是取消了点赞
                    commentsReply.setLikeState(i2);
                }else{
                    commentsReply.setLikeState(0);
                }
            }
        }
        commentsInfo.setCommentsReplies(list);
        return commentsInfo;
    }


}
